package com.steinwurf.hdcjoinwifi;

import ohos.aafwk.content.IntentParams;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.wifi.WifiDevice;
import ohos.wifi.WifiEvents;
import ohos.wifi.WifiLinkedInfo;

import java.util.logging.Level;
import java.util.logging.Logger;

public class CheckSSIDBroadcastReceiver extends CommonEventSubscriber {
    private static final String TAG = "qinjinchuan_CheckSSIDBroadcastReceiver";

    public interface SSIDFoundListener {
        void SSIDFound();

        void wifiEnabled();
    }

    private String mSSID;
    private SSIDFoundListener mSSIDFoundListener;
    private WifiDevice mWifiDevice;

    CheckSSIDBroadcastReceiver(CommonEventSubscribeInfo info, WifiDevice wifiDevice, String SSID) {
        super(info);
        mSSID = SSID;
        mWifiDevice = wifiDevice;
    }

    void setSSIDFoundListener(SSIDFoundListener listener) {
        mSSIDFoundListener = listener;
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        if (commonEventData == null || commonEventData.getIntent() == null) {
            return;
        }
        String action = commonEventData.getIntent().getAction();
        IntentParams params = commonEventData.getIntent().getParams();
        if (params == null) {
            return;
        }
        if (action.equals(WifiEvents.EVENT_ACTIVE_STATE)) {
            int wifiState = (int) params.getParam(WifiEvents.PARAM_ACTIVE_STATE);
            if (wifiState == WifiEvents.STATE_ACTIVE) {
                Logger.getLogger(TAG).log(Level.INFO, "Receive WifiEvents.STATE_ACTIVE %{public}d", wifiState);
                mSSIDFoundListener.wifiEnabled();
            } else if (wifiState == WifiEvents.STATE_INACTIVE) {
                Logger.getLogger(TAG).log(Level.INFO, "Receive WifiEvents.STATE_INACTIVE %{public}d", wifiState);
            } else {
                Logger.getLogger(TAG).log(Level.INFO, "Unknown wifi state");
            }
        } else if (action.equals(WifiEvents.EVENT_CONN_STATE)) {
            int connState = (int) params.getParam(WifiEvents.PARAM_CONN_STATE);
            if (connState == WifiEvents.STATE_CONNECTED) {
                Logger.getLogger(TAG).log(Level.INFO, "Receive WifiEvents.STATE_ACTIVE %{public}d", connState);
                WifiLinkedInfo wifiInfo = mWifiDevice.getLinkedInfo().get();
                if (wifiInfo != null && wifiInfo.getSsid().equals(mSSID)) {
                    if (mSSIDFoundListener != null) {
                        mSSIDFoundListener.SSIDFound();
                        mSSIDFoundListener = null;
                    }
                }
            } else if (connState == WifiEvents.STATE_DISCONNECTED) {
                Logger.getLogger(TAG).log(Level.INFO, "Receive WifiEvents.STATE_INACTIVE %{public}d", connState);
            } else {
                Logger.getLogger(TAG).log(Level.INFO, "Unknown wifi state, connState  = " + connState);
            }
        }
    }
}
